package com.cssw.fyzb.web.controller;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.web.dto.LogDto;
import com.cssw.fyzb.web.service.LoginService;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;


/**
 * @Auther: zzf
 * @Date: 2020/12/1 18:23
 * @Description:
 */

@ApiModel("角色相关接口")
@RestController
@RequestMapping("/log")
public class LoginController {

    @Autowired
    private LoginService loginService;

//    @RequestMapping(
//            value = "/getCaptcha", desc = "获取图形码", method = RequestMethod.GET
//    )
//    public void getCaptcha() throws Exception {
//        AppServerCore.inst()
//                .getRenderFactory().getCaptchaRender().setContext(getRequest(), getResponse()).render();
//    }

    @ApiOperation(value = "登录步骤根据密码登录")
    @RequestMapping(value = "/loginVerifyPwd", method = RequestMethod.POST)
    public AjaxData loginVerifyPwd(@RequestBody LogDto dto, BindingResult result) throws Exception{
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return loginService.loginVerifyPwd(dto.getOperCode(), dto.getOperPwd());
    }

    @ApiOperation(value = "调试接口")
    @PostMapping(value = "/test")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "a", value = "页码", paramType = "form", dataType = "string", required = true),
//            @ApiImplicitParam(name = "b", value = "每页条数", paramType = "form", dataType = "string", required = true),
//            @ApiImplicitParam(name = "c", value = "昵称", paramType = "form", dataType = "string", required = false),
//    })
    public AjaxPage test(@RequestBody HashMap a, String c, String b) {
        HashMap param = new HashMap();
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select * from cmf_gift ");
        Page page= Db.paginate(1, 10, sqlParse);
        List l = page.getList();
        return AjaxPage.success(page.getList(),page.getTotalPage(),page.getPageSize());
    }

}
